﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.Collections.Generic;
using System.Data;
using System.Linq;

#endregion

namespace ScrumTable.Common.Data
{
    /// <summary>
    /// Provides the additional functionalities required for ADO.NET DataRow 
    /// management.
    /// </summary>
    public static class DataRowUtil
    {
        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the primary key values of the current row.
        /// </summary>
        /// <param name="thisPtr">Specifies the pointer to the DataRow instance.</param>
        public static object[] GetPrimaryKeys(this DataRow thisPtr)
        {
            PreCondition.AssertNotNull(thisPtr, "thisPtr");

            object[] pkValues = new object[thisPtr.Table.PrimaryKey.Length];
            
            if (pkValues.Length > 0)
            {
                int counter = 0;
                foreach (DataColumn dataValueCol in thisPtr.Table.PrimaryKey)
                {
                    pkValues[counter++] = thisPtr[dataValueCol];
                }
            }
            return pkValues;
        }

        /// <summary>
        /// Gets a typed enumerable of values.
        /// </summary>
        /// <param name="thisPtr">Specifies the pointer to the DataRow instance.</param>
        public static IEnumerable<T> GetColumns<T>(this DataRow thisPtr)
        {
            PreCondition.AssertNotNull(thisPtr, "thisPtr");

            return thisPtr.ItemArray.Cast<T>();
        }

        #endregion
    }
}